package api

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"ftTest/app/dao"
	"ftTest/app/model"
	"sync"

	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/gcron"
	"github.com/gogf/gf/os/gfile"
	"github.com/gogf/gf/os/glog"
	"github.com/gogf/gf/util/gconv"
)

var Hello = &helloApi{}

type helloApi struct{}

// Index is a demonstration route handler for output "Hello World!".
func (*helloApi) Index(r *ghttp.Request) {
	ctx := r.Context()
	ahdm := r.GetString("ahdm")
	result, _ := dao.EAJ.Ctx(ctx).Where(dao.EAJ.Columns.AHDM, ahdm).FindOne()
	var eaj *model.EAJ
	gconv.Scan(result, &eaj)
	ej, _ := json.Marshal(eaj)
	var out bytes.Buffer
	json.Indent(&out, ej, "", "\t")
	r.Response.WriteJson(out.String())
}

func (*helloApi) First(r *ghttp.Request) {
	fb := gfile.GetBytes("E:\\BaiduNetdiskDownload\\powerdesigner1029.zip")
	r.Response.Write(fb)
}

func (*helloApi) Play(r *ghttp.Request) {
	ctx := r.Context()
	var eaj *model.EAJ
	r.Parse(&eaj)
	e, _ := dao.EAJ.Ctx(r.Context()).WhereLike(dao.EAJ.Columns.AHDM, eaj.AHDM+"%").FindAll()
	var wg sync.WaitGroup
	for _, v := range e {
		wg.Add(1)
		go func(ahdm string) {
			glog.Info("ahdm------------------------",ahdm,"========================================")
			re, _ := dao.EAJ.Ctx(ctx).WherePri(ahdm).FindOne()
			var pe *model.EAJ
			gconv.Scan(re, &pe)
			glog.Info(pe)
			ezl,_ := dao.EAJZXHDLIST.Ctx(ctx).Where(dao.EAJZXHDLIST.Columns.AHDM,ahdm).FindOne()
			if(ezl.IsEmpty()){
				fmt.Printf("eaj_zxhd_list.ahdm=%s is empty\n",ahdm)
			}else{
				var pEzl *model.EAJZXHDLIST
				gconv.Struct(ezl,&pEzl)
				glog.Info(*pEzl)
			}
			wg.Done()
		}(gconv.String(v["AHDM"]))
	}
	wg.Wait()
	glog.Info("end---------------------------------------------------")
	glog.Info("ahdm------------------------","end","--------------------------------------")

	gcron.Remove("firstcron")
	gcron.Add("*/20 * * * * *",func() {
		glog.Info("----------------------------cron start-----------------------------")
		for _, v := range e {
			re, _ := dao.EAJ.Ctx(context.TODO()).WherePri(v["AHDM"]).FindOne()
			if(!re.IsEmpty()){
				var pe *model.EAJ
				gconv.Scan(re, &pe)
				glog.Info(pe)
			}
		}
		glog.Info("----------------------------cron end-------------------------------------")
	},"firstcron")
	
	r.Response.Write(e)
}

